package com.techshinogongjian.fp.func;

import com.techshinogongjian.fingerprint.TCFP;
import com.techshinogongjian.fp.comm.HIDComm;
import com.techshinogongjian.fp.comm.MsgCallback;
import com.fingerUsb.FingerResult;
import com.techshinogongjian.fp.util.FpConst;

/**
 * HID协议功能层
 * 
 * @author wangzhi
 */
public class HIDFuncImpl extends IFingerprintFunc {

	private HIDComm mComm;

	public HIDFuncImpl(HIDComm comm) {
		mComm = comm;
	}

	@Override
	public FingerResult tcGetDevFWInfo() {
		if (isBusy()) {
			return new FingerResult(FpConst.TCY_BUSY);
		}

		mComm.setBusy(true);
		FingerResult result = mComm.getDevInfo();
		mComm.setBusy(false);
		return result;
	}

	@Override
	public int tcDevDetect(int nPort, String sPort) {
		return 0;
	}

	@Override
	public FingerResult tcGetDevSn() {
		if (isBusy()) {
			return new FingerResult(FpConst.TCY_BUSY);
		}

		mComm.setBusy(true);
		FingerResult result = mComm.getDevSn();
		mComm.setBusy(false);
		return result;
	}

	@Override
	public FingerResult tcGetImage(int nTimeout) {
		if (isBusy()) {
			return new FingerResult(FpConst.TCY_BUSY);
		}

		mComm.setBusy(true);
		FingerResult result = mComm.tcGetImg(nTimeout);
		mComm.setBusy(false);
		return result;
	}

	@Override
	public int tcIsPressed(int nTimeout) {
		return 0;
	}

	@Override
	public FingerResult tcGetFeature(int nTimeout) {
		if (isBusy()) {
			return new FingerResult(FpConst.TCY_BUSY);
		}

		mComm.setBusy(true);
		FingerResult result = mComm.tcGetFeature(nTimeout);
		mComm.setBusy(false);
		return result;
	}

	@Override
	public FingerResult tcGetTemplate(int nTimeout) {
		return new FingerResult(FpConst.TCY_NSUP);
	}
	
	@Override
	public FingerResult tcSetEnrollImageCount(int newCount) {
		return new FingerResult(FpConst.TCY_NSUP);
	}
	
	@Override
	public FingerResult tcMatch(byte[] feature, byte[] template, int security) {
		if (!mComm.isInitOk()) {
			return new FingerResult(FpConst.DOG_NONE);
		}
		FingerResult result = new FingerResult();
		if (feature == null || template == null) {
			return result.setStatus(FpConst.TCY_PARA);
		}
		int score = TCFP.TCFP_FeatureMatch(feature, feature.length, template, template.length);
		result.setScore(score);
		if (score < 60) {
			return result.setStatus(FpConst.TCY_FAIL);
		}
		return result;
	}

	@Override
	public int tcCancel() {
		if (mComm.isBusy()) {
			mComm.setCancel();
		}
		return 0;
	}

	@Override
	public boolean isConnected() {
		if (isBusy()) {
			return true;
		}
		return mComm.isConnected();
	}

	@Override
	public FingerResult tcGetResult() {
		return new FingerResult(FpConst.TCY_NSUP);
	}

	public boolean isBusy() {
		return mComm.isBusy();
	}

	@Override
	public boolean isCancel() {
		return mComm.isCancel();
	}

	@Override
	public FingerResult tcSetSecurityLevel(int level) {
		return new FingerResult(FpConst.TCY_NSUP);
	}

	@Override
	public FingerResult tcVerify(long fid, MsgCallback callback, int timeout) {
		return new FingerResult(FpConst.TCY_NSUP);
	}

	@Override
	public FingerResult tcSetTemplate(long fid, byte[] data) {
		return new FingerResult(FpConst.TCY_NSUP);
	}
	
	@Override
	public FingerResult tcEnroll(MsgCallback callback, int timeout) {
		if (isBusy()) {
			return new FingerResult(FpConst.TCY_BUSY);
		}

		mComm.setBusy(true);
		FingerResult result = mComm.tcEnroll(timeout, callback);
		mComm.setBusy(false);
		return result;
	}

	@Override
	public FingerResult tcBooTL() {
		return new FingerResult(FpConst.TCY_NSUP);
	}
}
